package com.hrms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hrms.pojo.Department;
import com.hrms.pojo.Employee;
import com.hrms.service.DepartmentService;
import com.hrms.service.EmployeeService;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

	@Resource
	private EmployeeService employeeService;
	@Resource
	private DepartmentService departmentService;

	// 跳转到功能模块界面
	@RequestMapping("/emp_f")
	public String emp() {
		return "emp/emp_f";
	}

	// 跳转到alter页面
	@RequestMapping("/alter_emp")
	public String alterEmp(Model model) {
		List<Department> departList = departmentService.getDepartALL();
		model.addAttribute("departList", departList);
		return "emp/alter_emp";
	}

	// 跳转到add页面
	@RequestMapping("/add_emp")
	public String addPage(Model model) {
		List<Department> departList = departmentService.getDepartALL();
		model.addAttribute("departList", departList);
		return "emp/add_emp";
	}

	// 分页查找
	@RequestMapping("/limit")
	public String getEmpByLimit(HttpServletRequest req, Model model) {
		int pageSize = 4;// 每页显示4条
		Object obj = req.getSession().getAttribute("empAll");
		List<Employee> empAll = null;
		if (obj == null) {
			empAll = employeeService.getEmpAll();
			req.getSession().setAttribute("empAll", empAll);
		} else {
			empAll = (List<Employee>) obj;
		}
		int total = empAll.size();
		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
		int showPage = 0;
		try {
			showPage = Integer.parseInt(req.getParameter("showPage"));
			if (showPage <= 1)
				showPage = 1;
		} catch (NumberFormatException e) {
			showPage = 1;
		}
		if (showPage > totalPage)
			showPage = totalPage;
		int offset = (showPage - 1) * pageSize;
		int limit = showPage * pageSize;
		req.getSession().setAttribute("showPage", showPage);
		List<Employee> empList = employeeService.getEmpByLimit(offset, limit);
		model.addAttribute("empList", empList);
		return "emp/limit_emp";
	}

	// 根据id查找
	@RequestMapping(value = "/{id}/getid")
	public String getEmpById(@PathVariable("id") Integer emId, Model model) {
		// 判断id是否为null
		if (emId == null) {
			// 为空跳转
			return "forward:/error/error";
		}
		Employee emp = employeeService.getEmpById(emId);
		// 验证id是否存在
		if (emp == null) {
			// 不存在跳转
			return "forward:/error/error";
		}
		// 存进model
		model.addAttribute("emp", emp);
		// 返回页面
		return "emp/detail";
	}

	// 根据名字模糊查找
	@RequestMapping(value = "/getname", method = RequestMethod.POST)
	public String getEmpByName(HttpServletRequest req, Model model) {
		String emName = req.getParameter("emName");
		// 获取list
		List<Employee> empList = employeeService.getEmpByName(emName);
		model.addAttribute("empList", empList);
		return "emp/limit_by_name";
	}

	// 根据身份证号查找
	@RequestMapping(value = "/getidnum")
	public String getEmpByIdNumber(HttpServletRequest req, Model model) {
		String emIdNumber = req.getParameter("emIdNumber");
		Employee emp = employeeService.getEmpByIdNumber(emIdNumber);
		// 验证是否存在
		if (emp == null) {
			// 不存在跳转
			return "forward:/error/error";
		}
		// 存进model
		model.addAttribute("emp", emp);
		// 返回页面
		return "emp/detail";
	}

	// 查找所有
	@RequestMapping(value = "/list")
	public String getList(Model model) {
		List<Employee> empList = employeeService.getEmpAll();
		model.addAttribute("empList", empList);
		return "emp/list_emp";
	}

	// 添加员工信息
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addEmp(HttpServletRequest req) {
		String emName = req.getParameter("emName");// 姓名
		String emSex = req.getParameter("emSex");// 性别
		int emAge = Integer.parseInt(req.getParameter("emAge"));// 年龄
		String emIdNumber = req.getParameter("emIdNumber");// 身份照号
		if ("".equals(emIdNumber))
			emIdNumber = "******************";
		String emBorn = req.getParameter("emBorn");// 出生日期
		String emNation = req.getParameter("emNation");// 民族
		String emMarriage = req.getParameter("emMarriage");// 婚姻状况
		String emVisage = req.getParameter("emVisage");// 政治面貌
		String emNativePlace = req.getParameter("emNativePlace");// 籍贯
		String emTel = req.getParameter("emTel");// 电话
		String emAddress = req.getParameter("emAddress");// 地址
		String emGraduateSchool = req.getParameter("emGraduateSchool");// 毕业学校
		String emSpecialty = req.getParameter("emSpecialty");// 专业
		String emEducation = req.getParameter("emEducation");// 学历
		String emTypework = req.getParameter("emTypework");// 工种
		String emCreatetime = req.getParameter("emCreatetime");// 登记时间
		String emCreatename = req.getParameter("emCreatename");// 登记人
		String emBz = req.getParameter("emBz");// 备注
		String emDtIdStr = req.getParameter("emDtId");// 部门id
		int emDtId = 0;
		if (!"".equals(emDtIdStr))
			emDtId = Integer.parseInt(emDtIdStr);

		Employee emp = new Employee();
		emp.setEmName(emName);
		emp.setEmSex(emSex);
		emp.setEmAge(emAge);
		emp.setEmIdNumber(emIdNumber);
		emp.setEmBorn(emBorn);
		emp.setEmNation(emNation);
		emp.setEmMarriage(emMarriage);
		emp.setEmVisage(emVisage);
		emp.setEmNativePlace(emNativePlace);
		emp.setEmTel(emTel);
		emp.setEmAddress(emAddress);
		emp.setEmGraduateSchool(emGraduateSchool);
		emp.setEmSpecialty(emSpecialty);
		emp.setEmEducation(emEducation);
		emp.setEmTypework(emTypework);
		emp.setEmCreatetime(emCreatetime);
		emp.setEmCreatename(emCreatename);
		emp.setEmBz(emBz);
		emp.setEmDtId(emDtId);
		employeeService.addEmp(emp);
		return "emp/add_succ";
	}

	// 修改员工信息
	@RequestMapping(value = "/{emId}/alter", method = RequestMethod.POST)
	public String alterEmp(HttpServletRequest req, @PathVariable("emId") Integer emId) {
		if (emId == null) {
			return "forward:/error/error";
		}
		Employee emp = employeeService.getEmpById(emId);
		if (emp == null) {
			// 该id的员工不存在，跳转
			return "forward:/error/error";
		}
		String emName = req.getParameter("emName");// 姓名
		if ("".equals(emName))
			emName = emp.getEmName();
		String emSex = req.getParameter("emSex");// 性别
		if ("".equals(emSex))
			emSex = emp.getEmSex();
		String emAgeStr = req.getParameter("emAge");// 年龄
		int emAge = 0;
		if ("".equals(emAgeStr)) {
			emAge = emp.getEmAge();
		} else {
			emAge = Integer.parseInt(emAgeStr);
		}
		String emIdNumber = req.getParameter("emIdNumber");// 身份照号
		if ("".equals(emIdNumber))
			emIdNumber = emp.getEmIdNumber();
		String emBorn = req.getParameter("emBorn");// 出生日期
		if ("".equals(emBorn))
			emBorn = emp.getEmBorn();
		String emNation = req.getParameter("emNation");// 民族
		if ("".equals(emNation))
			emNation = emp.getEmNation();
		String emMarriage = req.getParameter("emMarriage");// 婚姻状况
		if ("".equals(emMarriage))
			emMarriage = emp.getEmMarriage();
		String emVisage = req.getParameter("emVisage");// 政治面貌
		if ("".equals(emVisage))
			emVisage = emp.getEmVisage();
		String emNativePlace = req.getParameter("emNativePlace");// 籍贯
		if ("".equals(emNativePlace))
			emNativePlace = emp.getEmNativePlace();
		String emTel = req.getParameter("emTel");// 电话
		if ("".equals(emTel))
			emTel = emp.getEmTel();
		String emAddress = req.getParameter("emAddress");// 地址
		if ("".equals(emAddress))
			emAddress = emp.getEmAddress();
		String emGraduateSchool = req.getParameter("emGraduateSchool");// 毕业学校
		if ("".equals(emGraduateSchool))
			emGraduateSchool = emp.getEmGraduateSchool();
		String emSpecialty = req.getParameter("emSpecialty");// 专业
		if ("".equals(emSpecialty))
			emSpecialty = emp.getEmSpecialty();
		String emEducation = req.getParameter("emEducation");// 学历
		if ("".equals(emEducation))
			emEducation = emp.getEmEducation();
		String emTypework = req.getParameter("emTypework");// 工种
		if ("".equals(emTypework))
			emTypework = emp.getEmTypework();
		String emCreatetime = req.getParameter("emCreatetime");// 登记时间
		if ("".equals(emCreatetime))
			emCreatetime = emp.getEmCreatetime();
		String emCreatename = req.getParameter("emCreatename");// 登记人
		if ("".equals(emCreatename))
			emCreatename = emp.getEmCreatename();
		String emBz = req.getParameter("emBz");// 备注
		if ("".equals(emBz))
			emBz = emp.getEmBz();
		String emDtIdStr = req.getParameter("emDtId");// 部门id
		int emDtId = 0;
		if ("".equals(emDtIdStr)) {
			emDtId = emp.getEmDtId();
		} else {
			emDtId = Integer.parseInt(emDtIdStr);
		}

		emp.setEmName(emName);
		emp.setEmSex(emSex);
		emp.setEmAge(emAge);
		emp.setEmIdNumber(emIdNumber);
		emp.setEmBorn(emBorn);
		emp.setEmNation(emNation);
		emp.setEmMarriage(emMarriage);
		emp.setEmVisage(emVisage);
		emp.setEmNativePlace(emNativePlace);
		emp.setEmTel(emTel);
		emp.setEmAddress(emAddress);
		emp.setEmGraduateSchool(emGraduateSchool);
		emp.setEmSpecialty(emSpecialty);
		emp.setEmEducation(emEducation);
		emp.setEmTypework(emTypework);
		emp.setEmCreatetime(emCreatetime);
		emp.setEmCreatename(emCreatename);
		emp.setEmBz(emBz);
		emp.setEmDtId(emDtId);

		employeeService.alterEmp(emp);
		return "emp/alter_succ";
	}

	// 根据id删除员工
	@RequestMapping(value = "/{emId}/remove")
	public String removeEmp(@PathVariable("emId") Integer emId) {
		if (emId == null) {
			return "forward:/error/error";
		}
		Employee emp = employeeService.getEmpById(emId);
		if (emp == null) {
			return "forward:/error/error";
		}
		employeeService.removeEmpById(emId);
		return "emp/rem_succ";
	}
}
